package com.zrfintech.common.enums;

import java.text.MessageFormat;

/**
 * Restful接口返回结果
 * 
 * @author geyuqi
 *
 */
public enum RestRetCodeEnum {
    SUCCESS(200, "success"),
    INTERNAL_ERROR(201, "internal error"),
    NO_AUTH(202, "no auth"),

    INVALID_SIGN(203, "invalid sign"),

    INSUFFICIENT_BALANCE(205, "insufficient balance"),

    PARAMETER_ERROR(400, "parameter error: {0}"),

    PARAMETER_ERROR2(400, "parameter error: {0}, {1}"),

    REQUEST_LIMIT_SYS(401, "sys exceeds the request limit"),

    REQUEST_LIMIT_ACCOUNT(402, "account exceeds the request limit"),

    REQUEST_LIMIT_ACCOUNT_API(403, "account-api exceeds the request limit");

    private int value;
    private String msg;

    RestRetCodeEnum(int value, String msg) {
        this.value = value;
        this.msg = msg;
    }

    public String toString() {
        return String.valueOf(value);
    }

    public int getValue() {
        return value;
    }

    public String getMsg() {
        return msg;
    }

    public String getMsg(Object arg) {
        Object[] args = new Object[] { arg };
        return getMsg(args);
    }

    public String getRetMsg(Object arg1, Object arg2) {
        Object[] args = new Object[] { arg1, arg2 };
        return getMsg(args);
    }

    public String getMsg(Object[] args) {
        Object[] nonNullArgs = args;
        for (int i = 0; i < args.length; i++) {
            if (args[i] == null) {
                if (nonNullArgs == args) {
                    nonNullArgs = (Object[]) args.clone();
                }
                nonNullArgs[i] = "null";
            }
        }
        return MessageFormat.format(getMsg(), nonNullArgs);
    }

    public static RestRetCodeEnum getRestRetCodeEnum(int value) {
        for (RestRetCodeEnum r : RestRetCodeEnum.values()) {
            if (r.getValue() == value) {
                return r;
            }
        }
        return null;
    }
}
